package com.fira.quartz.task;

import com.alibaba.fastjson.JSONObject;
import com.fira.common.constant.Constants;
import com.fira.common.core.domain.platform.dto.SysDictDataDTO;
import com.fira.common.core.domain.platform.vo.SysDictDataVO;
import com.fira.common.core.domain.platform.vo.SysDictTypeVO;
import com.fira.common.core.domain.saas.entity.SysUser;
import com.fira.common.utils.DateUtils;
import com.fira.common.utils.StringUtils;
import com.fira.common.utils.uuid.IdUtils;
import com.fira.edu.domain.*;
import com.fira.edu.domain.vo.StudentParentsVO;
import com.fira.edu.service.*;
import com.fira.platform.service.ISysDictDataService;
import com.fira.platform.service.ISysDictTypeService;
import com.fira.system.domain.SysNoticeMessage;
import com.fira.system.service.ISysNoticeMessageService;
import com.fira.system.service.ISysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

/**
 * 上课点名消息任务
 * @author tony
 * @date 2021-10-27
 */
@Component("msgTask")
public class EduMsgTask {
    private static final Logger log = LoggerFactory.getLogger(EduMsgTask.class);
    @Autowired
    private IEduClassRollCallService eduClassRollCallService;
    @Autowired
    private IEduRollcallMessageRefService eduRollcallMessageRefService;
    @Autowired
    private IEduStudentCourseService eduStudentCourseService;
    @Autowired
    private IEduStudentService eduStudentService;
    @Autowired
    private IEduParentService eduParentService;
    @Autowired
    private IEduClassService eduClassService;
    @Autowired
    private IEduCourseService eduCourseService;
    @Autowired
    private ISysDictTypeService sysDictTypeService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysNoticeMessageService sysNoticeMessageService;

    private static final String NOTICE_TYPE = "上课签到通知";
    private static final String MESSAGE_TYPE = "message_template";

    public void createMsg(){
        /**
         * 1、查询未在上课点名消息通知表中的上课点名信息
         * 2、生成点名消息
         * 3、生成上课点名消息通知信息
         */
        List<EduClassRollCall> rollCallList = eduClassRollCallService.selectRollCallForMsg();
        if(CollectionUtils.isNotEmpty(rollCallList)){
            rollCallList.forEach(item -> {
                addMsg(item);
            });
        }
    }

    private void addMsg(EduClassRollCall item){
        EduStudentCourse eduStudentCourse = eduStudentCourseService.selectEduStudentCourseById(item.getStudentCourseId());
        if(eduStudentCourse == null){
            return;
        }
        EduClass eduClass = eduClassService.selectEduClassById(eduStudentCourse.getClassId());
        EduStudent eduStudent = eduStudentService.selectEduStudentById(eduStudentCourse.getStudentId());
        EduCourse eduCourse = eduCourseService.selectEduCourseById(eduStudentCourse.getCourseId());
        List<StudentParentsVO> parentVOList = eduStudentService.selectStudentParents(eduStudentCourse.getStudentId());
        if(CollectionUtils.isNotEmpty(parentVOList)){
            parentVOList.forEach(parent -> {
                log.info("开始生成到课点名通知");
                EduParent eduParent = new EduParent();
                eduParent.setParentPhone(parent.getParentPhone());
                List<EduParent> eduParentList = eduParentService.selectEduParentList(eduParent);
                SysNoticeMessage sysNoticeMessage = new SysNoticeMessage();
                sysNoticeMessage.setId(IdUtils.fastSimpleUUID());
                sysNoticeMessage.setClassId(eduStudentCourse.getClassId());
                sysNoticeMessage.setNoticeType(NOTICE_TYPE);
                sysNoticeMessage.setMessageContent(getMsgContent(eduCourse,item,eduClass));
                sysNoticeMessage.setReceiverId(eduParentList.get(0).getId());
                sysNoticeMessage.setTenantId(eduStudent.getTenantId());
                sysNoticeMessage.setSenderId(eduClass.getHeadTeacherId());
                sysNoticeMessage.setSendTime(DateUtils.getNowDate());
                sysNoticeMessage.setReadStatus(Constants.STATUS_ZERO);
                sysNoticeMessage.setTitle(eduClass.getClassName()+NOTICE_TYPE);
                sysNoticeMessageService.insertSysNoticeMessage(sysNoticeMessage);
                //生成点名消息关系数据
                addRollCallMessageRef(item,sysNoticeMessage);
                log.info("生成到课点名通知结束");
            });
        }
    }

    private JSONObject getContentJSON(EduCourse eduCourse,EduClassRollCall rollCall,EduClass eduClass){
        JSONObject contentJson = new JSONObject();
        String courseName = eduCourse.getCourseName() ;
        if(StringUtils.isNotEmpty(eduCourse.getCourseFlag())){
            courseName = courseName + "-"+eduCourse.getCourseFlag();
        }
        contentJson.put("courseName",courseName);
        contentJson.put("toclassTime",DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,rollCall.getCreateTime()));
        contentJson.put("toClassStatus",rollCall.getToClassStatus());
        contentJson.put("teacher", eduClass.getTeacherName());
        return contentJson;
    }

    private String getMsgContent(EduCourse eduCourse,EduClassRollCall rollCall,EduClass eduClass){
        SysDictTypeVO sysDictTypeVO = sysDictTypeService.selectDictTypeByType(MESSAGE_TYPE);
        SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
        sysDictDataDTO.setDictType(sysDictTypeVO.getDictType());
        sysDictDataDTO.setDictLabel(NOTICE_TYPE);
        SysDictDataVO sysDictDataVO = sysDictDataService.selectDictDataList(sysDictDataDTO).get(0);
        String templateContent = sysDictDataVO.getDictValue();
        JSONObject jsonObject = getContentJSON(eduCourse,rollCall,eduClass);
        for(Iterator<String> keyIt = jsonObject.keySet().iterator(); keyIt.hasNext();){
            String key = keyIt.next();
            templateContent = templateContent.replace("{{"+key+"}}",jsonObject.getString(key));
        }
        return templateContent;
    }

    private void addRollCallMessageRef(EduClassRollCall rollCall,SysNoticeMessage sysNoticeMessage){
        EduRollcallMessageRef rollcallMessageRef = new EduRollcallMessageRef();
        rollcallMessageRef.setId(IdUtils.fastSimpleUUID());
        rollcallMessageRef.setMessageId(sysNoticeMessage.getId());
        rollcallMessageRef.setRollCallId(rollCall.getId());
        rollcallMessageRef.setNoticeStatus(Constants.STATUS_ONE);
        rollcallMessageRef.setCreateTime(DateUtils.getNowDate());
        eduRollcallMessageRefService.insertEduRollcallMessageRef(rollcallMessageRef);
    }
}
